package com.io.jdt;
import java.io.*;
/**
 * ʹ��BufferedReader��BufferedWriter �����ı��ļ�//���ܸ���ͼƬ����Ƶ�ļ�
 * @author ���ڲ�ͬ
 */
public class CopyFile {
	public static void main(String[] args) throws IOException {
		String str;
		FileReader fr; //FileReader����
		FileWriter fw;//FileWriter����
		BufferedReader br = null;//BufferedReader����
		BufferedWriter  bw = null;//BufferedWriter����
		File f =null;
		try {
			f= new File("D:\\helloworld.txt");
			f.isDirectory();//�ж��Ƿ���Ŀ¼
			fr=new FileReader("D:\\helloworld.txt");
			
			fw = new FileWriter("D:\\ѧϰ\\copy.txt");
			
			br =new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			
			while( (str=br.readLine())!=null ){
				bw.write(str);
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
				try {
					bw.flush();
					br.close();
					bw.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		File file = new File("D:\\ѧϰ");
		System.out.println(file.toString()+"\n");
		getAllFileNames ( file, 0 );
		getAllFileLength (file);
		//���ֽ�������ΪMB(���ֽ�)
		System.out.println("��ǰĿ¼�������ļ��ܴ�СΪ: "+ len / 1000000 +"MB ( "+len / 1000000000+"GB )" );
	}
	/**
	 * ���õݹ鷽ʽ����������Ŀ¼�µ���Ŀ¼���ļ�, �Լ������Լ�
	 * @param file
	 * @param level
	 */
	public static void getAllFileNames (File file, int level ){
		//������ļ�������ļ���С
		for (int i = 0; i < level; i++) {
			System.out.print("-\t ");
		}
		//����ļ���
		//System.out.println(file);
		System.out.println(file.getName());
		if( file == null || !file.exists() ){//���·������ļ�Ϊ�� �� û���ļ���Ŀ¼��//�˴��ǵݹ�ͷ, �ܹ�����
			return;
		}else{
			if( file.isDirectory() ){//�����Ŀ¼
				for ( File f : file.listFiles() ) {
					getAllFileNames( f , level +1 );// ÿѭ��һ��, ��ȼ�1
				}
			}
		}
	}
	/**���������ļ���С
	 * ���õݹ鷽ʽ����������Ŀ¼�µ���Ŀ¼���ļ��Ĵ�С, �Լ������Լ�
	 * @param file
	 * @param level
	 */
	private static long len ;//�����ļ����ܴ�С, ��λ: �ֽ�(Byte)
	public static void getAllFileLength(File file){
		//������ļ�������ļ���С
		if(file.isFile()){
			len += file.length();
		}
		if( file.isDirectory() || !file.exists() ){//�����Ŀ¼�����ѭ�������ļ�, �����ļ���С
			if( file == null || !file.exists() ){//�˴��ǵݹ�ͷ, �ܹ�����
					return;
			}else{
					for (  File f : file.listFiles() ) {
						getAllFileLength ( f );//�ݹ鿪ʼ
					}
			}
		}
	}
}
